import wepy from 'wepy'
export default class extends wepy.mixin {
    data = {
        goods_id: 0,
        goodsInfo: {},
        address: null
    }

    methods = {
        imgPreview(img) {
            wepy.previewImage({
                current: img, // 当前显示图片的http链接
                urls: this.goodsInfo.pics.map(x => x.pics_big) // 需要预览的图片http链接列表
            })
        },
        // 选择收货地址
        async getAddress() {
            var res = await wepy.chooseAddress().catch(err => err)
            console.log(res);
            if (res.errmsg !== 'chooseAddress:ok') {
                return wepy.baseToast("获取地址失败")
            }
            this.address = res
            // 异步操作并修改了数据必须调用$apply()
            this.$apply()

        },
        // 加入购物车
        addCart() {
            // 调用全局的方法 并将当前商品信息带过去
            this.$parent.addToCarts(this.goodsInfo)
            wepy.showToast({
                title: '已添加到购物车',
                icon: 'success'
            })
        }
    }
    onLoad(o) {
        console.log(o)
        this.goods_id = o.goods_id
        this.getGoodsInfo()
    }

    async getGoodsInfo() {
        var { data: res } = await wepy.get('/goods/detail', {
            goods_id: this.goods_id
        })
        if (res.meta.status !== 200) {
            return wepy.baseToast()
        }
        console.log(res)
        this.goodsInfo = res.message
        this.$apply()

    }

    computed = {
        addressStr() {
            if (this.address == null) {
                return '请选择收回地址'
            }
            return this.address.provinceName +
                this.address.cityName +
                this.address.countyName
        },

        // 从全局获得购物车数量
        total(){
            return this.$parent.globalData.detailPageTotal
        }
    }
}